(function () {
  var domPageContainer = document.querySelector('.container');

  var slideSize = 3;
  var pageAreaSize = 2 * slideSize + 1;

  // 创建分页
  function createPage(curPage, totalPage) {
    //创建首页
    var firstPage = document.createElement('a');
    firstPage.href = '';
    firstPage.innerHTML = '首页';
    firstPage.className = 'normal';
    domPageContainer.appendChild(firstPage);
    //创建上一页
    var lastPage = document.createElement('a');
    lastPage.href = '';
    lastPage.innerHTML = '上一页';
    lastPage.className = 'normal';
    domPageContainer.appendChild(lastPage);

    //创建页码
    var tempPageSize = pageAreaSize;
    if (pageAreaSize <= totalPage) {
      tempPageSize = pageAreaSize;
    } else {
      tempPageSize = totalPage;
    }

    var startPage = 1;
    if (pageAreaSize <= totalPage) {
      startPage = curPage - slideSize;
    } else {
      startPage = 1;
    }

    for (var i = startPage; i <= startPage + tempPageSize-1; i++) {
      var pageItem = document.createElement('a');
      pageItem.href = '';
      pageItem.innerHTML = i;
      if (i == curPage) {
        pageItem.className = 'active';
      } else {
        pageItem.className = 'normal';
      }
      domPageContainer.appendChild(pageItem);
    }

    //创建下页
    var nextPage = document.createElement('a');
    nextPage.href = '';
    nextPage.innerHTML = '下一页';
    nextPage.className = 'normal';
    domPageContainer.appendChild(nextPage);
    //创建尾页
    var endPage = document.createElement('a');
    endPage.href = '';
    endPage.innerHTML = '尾页';
    endPage.className = 'normal';
    domPageContainer.appendChild(endPage);
  }

  createPage(8, 15);
})();
